package net.wanho.customerManage.controller;

import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.commons.vo.AjaxResult;
import net.wanho.customerManage.po.BusinessSource;
import net.wanho.customerManage.po.Customer;
import net.wanho.customerManage.po.LinkMan;
import net.wanho.customerManage.po.WorkingField;
import net.wanho.customerManage.service.BusinessSourceService;
import net.wanho.customerManage.service.CustomerService;
import net.wanho.customerManage.service.LinkManService;
import net.wanho.customerManage.service.WorkingFieldService;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/CustomerServlet")
public class CustomerServlet extends BaseServlet<CustomerService> {
	private BusinessSourceService businessSourceService = BeanFactory.getBean(BusinessSourceService.class) ;
	private WorkingFieldService workingFieldService = BeanFactory.getBean(WorkingFieldService.class) ;
	private LinkManService linkManService = BeanFactory.getBean(LinkManService.class) ;
	/*
	 * 查询列表
	 */
	protected String getAllCustomer(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		try {
			List<Customer> customerList = super.baseService.findAll();
			request.setAttribute("customerList",customerList);
			//查询相关的数据 【查询所有 业务来源  所属行业】
			//查询所有的相关数据
			List<BusinessSource> businessSourceList = businessSourceService.findAll();
			List<WorkingField> workingFieldList = workingFieldService.findAll();
			//设置作用域中
			request.setAttribute("businessSourceList",businessSourceList);
			request.setAttribute("workingFieldList",workingFieldList);
			return "/WEB-INF/view/customerManage/customer/index.jsp" ;
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage()) ;
		}

	}

	/*
	 * 新增顾客
	 */
	protected AjaxResult addCustomer(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			//封装用户请求参数 到 pojo 实体类
			Customer customer = super.parseRequest(request, Customer.class);
			//调用业务方法实现 新增功能
			super.baseService.save(customer) ;
			return AjaxResult.success() ;
		} catch (ServiceException e) {
			e.printStackTrace();
			return AjaxResult.success() ;
		}
	}
	

	
	/*
	 * 跳转的新增顾客页面
	 */
	protected String getAllZd(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		try {
			//查询所有的相关数据
			List<LinkMan> linkManList = linkManService.findAll();
			List<BusinessSource> businessSourceList = businessSourceService.findAll();
			List<WorkingField> workingFieldList = workingFieldService.findAll();
			//设置作用域中
			request.setAttribute("linkManList",linkManList);
			request.setAttribute("businessSourceList",businessSourceList);
			request.setAttribute("workingFieldList",workingFieldList);
			//使用服务器内部跳转 到目标页面进行显示
    		return "/WEB-INF/view/customerManage/customer/create.jsp";
		}  catch (ServiceException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage()) ;
		}
	}
	/*
	 * 单个数据查询
	 */
	protected String selectByPrimaryKey(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		try {
			//判断 跳转的目标页面  是view.jsp  还是 update.jsp
			String type = request.getParameter("type");
			String customerId = request.getParameter("customerId");
			Customer customer = super.baseService.findById(customerId);
			request.setAttribute("customer",customer);
			//查看功能  【多表查询】
			if ("1".equals(type)){
				return "/WEB-INF/view/customerManage/customer/view.jsp" ;
			}
			//查询相关的数据 【查询所有  联系人  业务来源  所属行业】
			//查询所有的相关数据
			List<LinkMan> linkManList = linkManService.findAll();
			List<BusinessSource> businessSourceList = businessSourceService.findAll();
			List<WorkingField> workingFieldList = workingFieldService.findAll();
			//设置作用域中
			request.setAttribute("linkManList",linkManList);
			request.setAttribute("businessSourceList",businessSourceList);
			request.setAttribute("workingFieldList",workingFieldList);
			return "/WEB-INF/view/customerManage/customer/update.jsp";
		}  catch (ServiceException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage()) ;
		}
	}
	/*
	 * 修改
	 */
	protected AjaxResult updateCustomer(HttpServletRequest request,HttpServletResponse response) {
		try {
			//封装用户请求参数 到 pojo 实体类
			Customer customer = super.parseRequest(request, Customer.class);
			//调用业务方法实现 修改功能
			super.baseService.modify(customer) ;
			return AjaxResult.success() ;
		} catch (ServiceException e) {
			e.printStackTrace();
			return AjaxResult.success() ;
		}
	}
	
	/*
	 * 搜索
	 */
	protected String getSomeCustomer(HttpServletRequest request,HttpServletResponse response) throws ParseException, ServletException {
		try {
			String type = request.getParameter("type");
			String text = request.getParameter("text");
			text = URLDecoder.decode(text,"UTF-8") ;
			String texTime = request.getParameter("texTime");
			Map<String,String> condMap = new HashMap<>(3) ;
			condMap.put("type",type) ;
			condMap.put("text",text) ;
			condMap.put("texTime",texTime) ;
			List<Customer> customerList = super.baseService.findByCond(condMap) ;
			request.setAttribute("customerList",customerList);
			//查询相关的数据 【查询所有 业务来源  所属行业】
			//查询所有的相关数据
			List<BusinessSource> businessSourceList = businessSourceService.findAll();
			List<WorkingField> workingFieldList = workingFieldService.findAll();
			//设置作用域中
			request.setAttribute("businessSourceList",businessSourceList);
			request.setAttribute("workingFieldList",workingFieldList);
			return "/WEB-INF/view/customerManage/customer/index.jsp" ;
		} catch (ServiceException | UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage()) ;
		}
	}
}
